package com.xkdx.guangguang.fragment.shop.brandofshop;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.brand.branch.BranchFragment;
import com.xkdx.guangguang.shareclass.BrandDetail;

public class SingleFloorFragment extends Fragment {

	View view = null;
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	List<BrandDetail> list;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		list = FloorsOfShopFragment.list.get(getArguments().getInt("position"))
				.getList();

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.empty_photo)
				.showImageForEmptyUri(R.drawable.empty_photo)
				.showImageOnFail(R.drawable.empty_photo)
				.resetViewBeforeLoading().cacheOnDisc()
				.imageScaleType(ImageScaleType.EXACTLY)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater
				.inflate(R.layout.brand_of_singlefloor, container, false);
		init(view);

		return view;
	}

	private void init(View v) {
		Button back = (Button) v.findViewById(R.id.single_classification_back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				FragmentManager fm = getFragmentManager();
				fm.popBackStack();

			}
		});
		ListView listView = (ListView) v.findViewById(R.id.single_floor_list);
		MyAdapter adatper = new MyAdapter(getActivity());
		listView.setAdapter(adatper);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				FragmentTransaction ft = getFragmentManager()
						.beginTransaction();
				BranchFragment branchFragment = new BranchFragment();
				Bundle b = new Bundle();
				b.putString("shopID", String.valueOf(list.get(position).getBrandID()));
				branchFragment.setArguments(b);
				ft.replace(R.id.fragment, branchFragment);
				ft.addToBackStack(null);
				ft.commit();

			}
		});
		Button brandClassification = (Button) v
				.findViewById(R.id.single_classification);
		brandClassification.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				FragmentManager fm = getFragmentManager();
				fm.popBackStack();
				fm.popBackStack();

			}
		});
	}

	class MyAdapter extends BaseAdapter {

		// private ImageLoader imageLoader;
		String url;
		Context context;

		public MyAdapter(Context context) {
			super();
			this.context = context;
			// imageLoader = ImageLoader.getInstance(context);

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (list == null || list.size() == 0) {
				return null;
			}
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.shop_item, null);
				holder = new ViewHolder();
				holder.im = (ImageView) convertView
						.findViewById(R.id.series_item_image);
				holder.card_zhe = (ImageView) convertView
						.findViewById(R.id.card_zhe);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.card_zhe.setVisibility(View.GONE);
			if (list.get(position).getCard().equals("True")) {
				holder.card_zhe.setVisibility(View.VISIBLE);
			}
			url = list.get(position).getLogo();
			if (url != null) {
				imageLoader.displayImage(url, holder.im, options);
			}
			holder.name.setText(list.get(position).getShopName());

			return convertView;
		}

		class ViewHolder {
			TextView name;
			ImageView im, card_zhe;
		}
	}

}
